<template>
  <div class="app-container">
    <!-- 搜索区域 -->
    <el-form
      v-show="showSearch"
      ref="queryForm"
      :inline="true"
      :model="queryParams"
      label-width="100px"
    >
      <el-form-item label="设备名称" prop="deviceName">
        <el-input
          v-model="queryParams.deviceName"
          clearable
          placeholder="请输入设备名称"
          style="width: 240px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="设备编码" prop="deviceCode">
        <el-input
          v-model="queryParams.deviceCode"
          clearable
          placeholder="请输入设备编码"
          style="width: 240px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="产品名称" prop="productName">
        <el-input
          v-model="queryParams.productName"
          clearable
          placeholder="请输入产品名称"
          style="width: 240px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="属性名称" prop="attributeName">
        <el-input
          v-model="queryParams.attributeName"
          clearable
          placeholder="请输入属性名称"
          style="width: 240px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="上报时间">
        <el-date-picker
          v-model="dateRange"
          end-placeholder="结束日期"
          range-separator="至"
          start-placeholder="开始日期"
          style="width: 240px"
          type="daterange"
          value-format="yyyy-MM-dd"
        ></el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button icon="el-icon-search" size="mini" type="primary" @click="handleQuery"
        >搜索
        </el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <!-- 操作按钮区域 -->
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button icon="el-icon-download" plain size="mini" type="warning" @click="handleExport"
        >导出
        </el-button
        >
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <!-- 数据表格区域 -->
    <el-table v-loading="loading" :data="dataList">
      <el-table-column align="center" label="设备名称" prop="deviceName"/>
      <el-table-column align="center" label="设备编码" prop="deviceCode"/>
      <el-table-column align="center" label="产品名称" prop="productName"/>
      <el-table-column align="center" label="产品编码" prop="productCode"/>
      <el-table-column align="center" label="属性名称" prop="attributeName"/>
      <el-table-column align="center" label="属性编码" prop="attributeCode"/>
      <el-table-column align="center" label="数据类型" prop="attributeType"/>
      <el-table-column align="center" label="数据值" prop="dataValue"/>
      <el-table-column align="center" label="单位" prop="attributeUnit"/>
      <el-table-column align="center" label="上报时间" prop="reportTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.reportTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column :show-overflow-tooltip="true" align="center" label="备注" prop="remark"/>
    </el-table>

    <pagination
      v-show="total > 0"
      :limit.sync="queryParams.pageSize"
      :page.sync="queryParams.pageNum"
      :total="total"
      @pagination="getList"
    />
  </div>
</template>

<script>
import {listDeviceData} from "@/api/system/iot/device/data";

export default {
  name: "DeviceData",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 设备数据表格数据
      dataList: [],
      // 日期范围
      dateRange: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        deviceName: undefined,
        deviceCode: undefined,
        productName: undefined,
        attributeName: undefined
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询设备数据列表 */
    getList() {
      this.loading = true;
      listDeviceData(this.addDateRange(this.queryParams, this.dateRange)).then(response => {
        this.dataList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "/iot/device/data/export",
        {
          ...this.queryParams
        },
        `device_data_${new Date().getTime()}.xlsx`
      );
    }
  }
};
</script>

<style scoped>
.app-container {
  padding: 20px;
}
</style>
